 //环信注册接口
 function fnEaseRegister(_name, _pawssword) {
     var easeChat = api.require('easeChat'); //引用模块插件
     easeChat.easeRegister({
         username: _name,
         password: _pawssword
     }, function (ret, err) {
         //隐藏加载框
         hideLoading();
         if (ret && ret.status) {
             toast('即时通讯：注册成功', 3000);
             //关闭注册页面
             setTimeout(function () {
                 api.closeWin()
             }, 200);
         } else {
             toast("即时通讯：" + err.msg, 3000);
         }
     });
 }

 //环信登录接口
 function fnEaseChatLogin(_name, _pawssword, params) {
     var easeChat = api.require('easeChat');
     //fnLogout();//先退出再登录
     easeChat.login({
         username: _name,
         password: _pawssword,
         autoLogin: true
     }, function (ret, err) {
         //隐藏加载框
         if (ret && ret.status) {
             fnAccountListener();
         } else {
             toast("即时通讯：" + err.msg, 3000);
         }
     });
 }

 // 登录之后监听账号异常
 function fnAccountListener() {
     var easeChat = api.require('easeChat');
     easeChat.addAccountListener(function (ret) {
         if (ret.eventType === 'otherLogin') {
             toast("即时通讯：当前账号在其它设备登录,请从新登录");
             setTimeout(function () {
                 fnLogout();
             }, 300);
             return;
         }
         if (ret.eventType === 'remove') {
             toast("即时通讯：当前登录账号已被服务器删除,请联系管理员");
             setTimeout(function () {
                 fnLogout();
             }, 300);
             return;
         }
         if (ret.eventType === 'forbid') {
             toast("即时通讯：服务被禁用,请联系管理员");
             setTimeout(function () {
                 fnLogout();
             }, 300);
             return;
         }
     });
 }

 //退出登录
 function fnLogout() {
     var easeChat = api.require('easeChat');
     easeChat.logout(function (ret, err) {
        //  hideLoading();
         if (ret.status) {
             toast("已退出即时聊天");
             //  api.closeWin()
             //  api.closeToWin({
             //      name: './html/main'
             //  });
             //  api.closeToWin({
             //      name: 'main'
             //  });
             //  api.sendEvent({
             //      name: 'closeChat',
             //  });
         }
     });
 }


 //新建或获取一个会话 安卓同样支持
 function fnGetConversation(conversationId_, type_) {
     var easeChat = api.require('easeChat');
     easeChat.getConversation({
         conversationId: conversationId_,
         type: type_,
         ifCreate: true
     }, function (ret, err) {
         if (ret.status) {
             console.log(JSON.stringify(ret))
         } else {
             toast("即时通讯：" + err.msg);
         }
     });
 }

 //点击消息已读
 function fnMarkAllMessagesAsRead(conversationId_) {
     var easeChat = api.require('easeChat');
     easeChat.markAllMessagesAsRead({
         conversationId: conversationId_,
         type: "chat"
     }, function (ret, err) {
         if (ret.status) {
             //alert(JSON.stringify(ret));
         } else {
             toast("即时通讯：" + err.msg);
         }
     });
 }

 //将指定消息设置为已读
 function fnMarkMessageAsRead(conversationId_, chatType_, messageId_) {
     var easeChat = api.require('easeChat');
     easeChat.markMessageAsRead({
         conversationId: conversationId_,
         type: chatType_,
         messageId: messageId_
     }, function (ret, err) {
         if (!(ret && ret.status == true)) {
             api.toast({
                 msg: '即时通讯：' + JSON.stringify(err),
                 duration: 2000,
                 location: 'bottom'
             });
         }
         //  /  api.alert({ msg:JSON.stringify(ret)});
     });
 }



 /**
  * 获取字符串的哈希值
  * @param {String} str
  * @param {Boolean} caseSensitive
  * @return {Number} hashCode
  */
 function getHashCode(str, caseSensitive) {
     if (!caseSensitive) {
         str = str.toLowerCase();
     }
     // 1315423911=b'1001110011001111100011010100111'
     var hash = 1315423911,
         i, ch;
     for (i = str.length - 1; i >= 0; i--) {
         ch = str.charCodeAt(i);
         hash ^= ((hash << 5) + ch + (hash >> 2));
     }

     return (hash & 0x7FFFFFFF);
 }
